package com.ruoyi.cms.navItem.service.Impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.cms.navType.domain.CmsNavType;
import com.ruoyi.cms.navType.mapper.CmsNavTypeMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cms.navItem.mapper.CmsNavItemMapper;
import com.ruoyi.cms.navItem.domain.CmsNavItem;
import com.ruoyi.cms.navItem.service.ICmsNavItemService;

import javax.annotation.Resource;

/**
 * 菜单项目Service业务层处理
 *
 * @author linyang
 * @date 2025-07-28
 */
@Service
public class CmsNavItemServiceImpl implements ICmsNavItemService {
    @Resource
    private CmsNavItemMapper cmsNavItemMapper;


    @Resource
    private CmsNavTypeMapper cmsNavTypeMapper;

    /**
     * 查询菜单项目
     *
     * @param id 菜单项目主键
     * @return 菜单项目
     */
    @Override
    public CmsNavItem selectCmsNavItemById(Long id) {
        return cmsNavItemMapper.selectCmsNavItemById(id);
    }

    /**
     * 查询菜单项目列表
     *
     * @param cmsNavItem 菜单项目
     * @return 菜单项目
     */
    @Override
    public List<CmsNavItem> selectCmsNavItemList(CmsNavItem cmsNavItem) {
        return cmsNavItemMapper.selectCmsNavItemList(cmsNavItem);
    }

    /**
     * 新增菜单项目
     *
     * @param cmsNavItem 菜单项目
     * @return 结果
     */
    @Override
    public int insertCmsNavItem(CmsNavItem cmsNavItem) {
        cmsNavItem.setCreateTime(DateUtils.getNowDate());
        return cmsNavItemMapper.insertCmsNavItem(cmsNavItem);
    }

    /**
     * 修改菜单项目
     *
     * @param cmsNavItem 菜单项目
     * @return 结果
     */
    @Override
    public int updateCmsNavItem(CmsNavItem cmsNavItem) {
        cmsNavItem.setUpdateTime(DateUtils.getNowDate());
        return cmsNavItemMapper.updateCmsNavItem(cmsNavItem);
    }

    /**
     * 批量删除菜单项目
     *
     * @param ids 需要删除的菜单项目主键
     * @return 结果
     */
    @Override
    public int deleteCmsNavItemByIds(Long[] ids) {
        return cmsNavItemMapper.deleteCmsNavItemByIds(ids);
    }

    /**
     * 删除菜单项目信息
     *
     * @param id 菜单项目主键
     * @return 结果
     */
    @Override
    public int deleteCmsNavItemById(Long id) {
        return cmsNavItemMapper.deleteCmsNavItemById(id);
    }


    /**
     * 修改导航链接状态
     *
     * @param cmsNavItem
     * @return
     */
    @Override
    public int updateCmsNavItemStatus(CmsNavItem cmsNavItem) {
        LambdaUpdateWrapper<CmsNavItem> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(CmsNavItem::getStatus, cmsNavItem.getStatus());
        lambdaUpdateWrapper.set(CmsNavItem::getUpdateBy, cmsNavItem.getUpdateBy());
        lambdaUpdateWrapper.set(CmsNavItem::getUpdateTime, DateUtils.getNowDate());
        lambdaUpdateWrapper.eq(CmsNavItem::getId, cmsNavItem.getId());
        return cmsNavItemMapper.update(null, lambdaUpdateWrapper);
    }


    /**
     * 根据导航分类的key查询对应的CmsNavItem列表
     *
     * @param IdentityKey
     * @return
     */
    public List<CmsNavItem> selectCmsNavItemListByCmsNavTypeKey(String IdentityKey) {
        CmsNavType cmsNavType = cmsNavTypeMapper.selectCmsNavTypeByIdentityKey(IdentityKey);
        if (ObjectUtil.isEmpty(cmsNavType)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<CmsNavItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CmsNavItem::getTypeId, cmsNavType.getId());
        //TODO 这边声明一个常量
        lambdaQueryWrapper.eq(CmsNavItem::getStatus, "0");//暂时只显示有效的
        lambdaQueryWrapper.orderByAsc(CmsNavItem::getOrderNum);//临时排序，好像没有用
        return cmsNavItemMapper.selectList(lambdaQueryWrapper);
    }
}
